<?php

namespace Modules\System\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Modules\System\Models\ApiInfoModel;
use Modules\System\Services\Mds\MDSString;

class ApiInfo
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle(Request $request,Closure $next){

        if(!isset($_POST['mds_curl_type'])){
            if(isset($_GET['mds_curl_type'])){
                $mds_curl_type = $_GET['mds_curl_type'];
            }else{
                $mds_curl_type = -1;
            }
        }else{
            $mds_curl_type = $_POST['mds_curl_type'];
        }

        //print_r($mds_curl_type);exit;
        //不是后台记录模拟请求的才记录
        if($mds_curl_type != 1){

            //获取当前的域名:
            $data['url'] = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            //print_r($_SERVER['REQUEST_URI']);exit;

            //d($_SERVER['REQUEST_URI']);
//            var_dump(strpos(strtolower($_SERVER['REQUEST_URI']),"dispatch/driver/drivermap"));
//            exit;

            $noUrl = [
                "/dispatch/driver/drivermap",
                "/chinadmin/home/Timing/app_status",
            ];

            //in_array($REQUEST_URI,$AuthServices::$needMenu)
//            var_dump(MDSString::arrContain($noUrl,$_SERVER['REQUEST_URI']));
//            exit;

            //只记录特定的dispatch接口模块
            if(
            (strpos(strtolower($_SERVER['REQUEST_URI']),"/admin/") === false)
                && ! MDSString::arrContain($noUrl,$_SERVER['REQUEST_URI'])
            ){
                if(empty($_POST)){
                    $data['method'] = "GET";
                    //$data['data'] = json_encode($_GET);
                    $data['data'] = "{}";

                }else{
                    $data['method'] = "POST";
                    $data['data'] = json_encode($_POST);
                }

                if($data['data'] == "[]"){
                    $data['data'] = "{}";
                }

                $data['created_at'] = date("Y-m-d H:i:s");

                $re = ApiInfoModel::insert($data);
            }

        }
        return $next($request);

    }
}
